ifeq (,$(CNC_INSTALL_DIR))
CNC_INSTALL_DIR2=$(shell dirname $(CURDIR))
CNC_INSTALL_DIR=$(shell dirname $(CNC_INSTALL_DIR2))
endif

ARCH := intel64

SOURCES_C = $(wildcard *.c)
SOURCES_CPP = $(wildcard *.cpp)

TARGETS := stencil

DEST_OBJS_C :=$(SOURCES_C:.c=.o)
DEST_OBJS_CPP :=$(SOURCES_CPP:.cpp=.opp)

CXX = clang++
OPTCXX := -std=c++11 -O3 #-openmp
OPT := -O3 #-openmp
CC = clang

ifeq ($(GCC),1)
OPT = -fast
CXX = g++
OPT := -O3 -fopenmp
OPTCXX := -O3 -fopenmp -std=c++11 
CC=gcc
endif

ifeq ($(ICC),1)
CXX = icpc
OPTCXX := -O3 -fp-model precise -openmp -vec_report=3
CC = icc
OPT := -O3 -fp-model precise -openmp
endif

PAPI_INC_DIR=/usr/local/include/
PAPI_LIB_DIR=/usr/local/lib/

CNC_RUN_OPTIONS :=
ifeq ($(DIST),1)
CNC_RUN_OPTIONS += -D_DIST_
endif
ifeq ($(PAPI),1)
CNC_RUN_OPTIONS += -D_PAPI_
endif
ifeq ($(HUGE_TLB),1)
CNC_RUN_OPTIONS += -DHUGE_TLB
endif

all:  stencil

%.opp: %.cpp
	$(CXX) -c -I$(CNC_INSTALL_DIR)/include -I$(PAPI_INC_DIR) $(OPTCXX) $(CNC_RUN_OPTIONS) -o $@ $< 

%.o: %.c
	$(CC) -c -I$(CNC_INSTALL_DIR)/include -I$(PAPI_INC_DIR) $(OPT) $(CNC_RUN_OPTIONS) -o $@ $< 

stencil: $(DEST_OBJS_CPP) $(DEST_OBJS_C)
	$(CXX) -o $@ $(DEST_OBJS_C) $(DEST_OBJS_CPP) -std=c++0x -lcnc -ltbb -ltbbmalloc -lpthread -lrt -L$(PAPI_LIB_DIR) -lpapi -fopenmp

clean:
	make clean -C simple_expression_template
	rm -f $(TARGETS) $(DEST_OBJS_C) $(DEST_OBJS_CPP) *~ a.out dmp err */*~
